package com.dingreading.cloud.mall.service.impl;

import com.dingreading.cloud.common.util.EnumType;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.mall.dto.CouponStaffDto;
import com.dingreading.cloud.mall.entity.CouponStaff;
import com.dingreading.cloud.mall.entity.table.CouponStaffTableDef;
import com.dingreading.cloud.mall.entity.table.OrderFormTableDef;
import com.dingreading.cloud.mall.mapper.CouponStaffMapper;
import com.dingreading.cloud.mall.mapper.OrderFormMapper;
import com.dingreading.cloud.mall.service.CouponStaffService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CouponStaffServiceImpl extends BaseServiceImpl<CouponStaffMapper, CouponStaff, CouponStaffTableDef> implements CouponStaffService {

    @Override
    protected CouponStaffTableDef getTable() {
        return CouponStaffTableDef.couponStaff;
    }

    @Resource
    private OrderFormMapper orderFormMapper;


    @Override
    public Page<CouponStaff> pageList(PageUtil pageUtil, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.couponName.like(keyword));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.enabled.desc(), table.id.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public CouponStaff getStaffCoupon(String storeUid, String staffUid, Long id) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.staffUid.eq(staffUid))
                .and(table.enabled.eq(1))
                .and(table.usableMoney.gt(0))
                .and(table.id.eq(id));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public CouponStaffDto dtoGetStaffCoupon(String storeUid, String staffUid, Long id) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.staffUid.eq(staffUid))
                .and(table.enabled.eq(1))
                .and(table.usableMoney.gt(0))
                .and(table.id.eq(id));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition);
        return getMapper().selectOneWithRelationsByQueryAs(wrapper, CouponStaffDto.class);
    }

    @Override
    public boolean updateMoney(String storeUid, String staffUid, Long id, double usedMoney, double usableMoney) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.staffUid.eq(staffUid))
                .and(table.id.eq(id))
                .and(table.enabled.eq(1))
                .and(table.usableMoney.gt(0));
        return UpdateChain.of(CouponStaff.class)
                .set(table.usedMoney, usedMoney)
                .set(table.usableMoney, usableMoney)
                .where(condition)
                .update();
    }

    @Override
    public List<CouponStaffDto> getOrderUseCouponV2(String storeUid, String staffUid, double useCouponPrice, List<Long> goodsIdList, List<Long> categoryIdList) {
        OrderFormTableDef qOrderForm = OrderFormTableDef.orderForm;
        QueryCondition formCondition = QueryCondition.createEmpty();
        formCondition.and(qOrderForm.storeUid.eq(storeUid))
                .and(qOrderForm.staffUid.eq(staffUid))
                .and(qOrderForm.status.in(EnumType.OrderForm.WAIT_CONFIRM.getType(), EnumType.OrderForm.WAIT_PAY.getType(), EnumType.OrderForm.APPLY_BANKPAY.getType()))
                .and(qOrderForm.couponStaffId.isNotNull());
        QueryWrapper formWrapper = QueryWrapper.create()
                .select(qOrderForm.couponStaffId)
                .from(qOrderForm)
                .where(formCondition);
        List<Long> couponStaffIds = orderFormMapper.selectObjectListByQueryAs(formWrapper, Long.class);

        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.staffUid.eq(staffUid))
                .and(table.enabled.eq(1))
                .and(table.usableMoney.gt(0));

        QueryCondition typeCondition = table.typeId.eq(1L)
                .or(table.typeId.eq(2L).and(table.conformMoney.le(useCouponPrice)));
        if (CollectionUtils.isNotEmpty(goodsIdList)) {
            String sql = goodsIdRegexpSql(goodsIdList, categoryIdList);
            typeCondition.or(table.typeId.eq(3L).and(sql));
        }
        condition.and(typeCondition);

        if (CollectionUtils.isNotEmpty(couponStaffIds))
            condition.and(table.id.notIn(couponStaffIds));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.usableMoney.desc());
        return getMapper().selectListWithRelationsByQueryAs(wrapper, CouponStaffDto.class);
    }

    private String goodsIdRegexpSql(List<Long> goodsIdList, List<Long> categoryIdList) {
        String sql = "";
        if (CollectionUtils.isNotEmpty(goodsIdList)) {
            String joinIds = goodsIdList.stream().map(String::valueOf).collect(Collectors.joining("|"));
            sql = "goods_ids REGEXP '(^|,)(" + joinIds + ")(,|$)'";
        }
        if (CollectionUtils.isNotEmpty(categoryIdList)) {
            String joinIds = categoryIdList.stream().map(String::valueOf).collect(Collectors.joining("|"));
            String categoryIdSql = "goods_category_ids REGEXP '(^|,)(" + joinIds + ")(,|$)'";
            if (StringUtils.isNotBlank(sql)) {
                sql = "(" + sql + " or " + categoryIdSql + ")";
            } else {
                sql = categoryIdSql;
            }
        }

        return sql;
    }


    @Override
    public List<CouponStaffDto> getOrderNotUseCouponV2(String storeUid, String staffUid, List<Long> couponStaffIds) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.staffUid.eq(staffUid))
                .and(table.enabled.eq(1))
                .and(table.usableMoney.gt(0))
                .and(table.id.notIn(couponStaffIds));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.usableMoney.desc());

        return getMapper().selectListWithRelationsByQueryAs(wrapper, CouponStaffDto.class);
    }

    @Override
    public List<CouponStaff> staffUseCouponList(String storeUid, String staffUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.staffUid.eq(staffUid))
                .and(table.enabled.eq(1))
                .and(table.usableMoney.gt(0));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());
        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public List<CouponStaff> staffUseGoodsCouponList(String storeUid, String staffUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.staffUid.eq(staffUid))
                .and(table.enabled.eq(1))
                .and(table.usableMoney.gt(0))
                .and(table.typeId.eq(3));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());
        return getMapper().selectListByQuery(wrapper);
    }

}
